package cn.eoeit.alone.phoneImport;

import cn.eoeit.alone.phoneImport.activity.CreatePhoneActivity;
import cn.eoeit.alone.phoneImport.fragment.HomeFragment;
import cn.eoeit.alone.phoneImport.fragment.MineFragment;
import cn.eoeit.alone.phoneImport.fragment.PhoneFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity  extends FragmentActivity {

	private FragmentTabHost mTabHost;
	
	Boolean isBack=true;
	private Class[] mFragments = new Class[] { HomeFragment.class,
			  PhoneFragment.class,
			MineFragment.class };
	private int[] mTabSelectors = new int[] { R.drawable.main_bottom_tab_home, R.drawable.main_bottom_tab_cart,
			R.drawable.main_bottom_tab_mine };
	private String[] mTabSpecs = new String[] { "shouye", "cart", "mine" };
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		((SysApplication)getApplicationContext()).addActivity(this);
		setContentView(R.layout.activity_main);
		mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
		addTabFragment();
	}
	
	private void addTabFragment() {
		for (int i = 0; i < mFragments.length; i++) {
			View tabIndicator = getLayoutInflater().inflate(
					R.layout.tab_indicator, null);
			ImageView imageView = (ImageView) tabIndicator
					.findViewById(R.id.imageView1);
			imageView.setImageResource(mTabSelectors[i]);
			mTabHost.addTab(
					mTabHost.newTabSpec(mTabSpecs[i])
							.setIndicator(tabIndicator), mFragments[i], null);
		}
	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			//����ǰ������ҳ�����ȷ�����ҳ
			if (mTabHost.getCurrentTab() != 0) {
				mTabHost.setCurrentTab(0);
				return false;
			}
			// ˫���������棬Ĭ�Ϸ���true������finish()
			if (!isBack) {
				isBack = true;
				Toast.makeText(this, "�ٰ�һ�η��ؼ�ص�����", Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(Intent.ACTION_MAIN);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.addCategory(Intent.CATEGORY_HOME);
				startActivity(intent);
				return false;
			}

		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void activeCreatePhoneRecord() {
		startActivity(new Intent(this, CreatePhoneActivity.class));
	}
}
